"""
_*_ : utf-8 _*_ 
@Time: 2023/11/2 21:20
@Author: LWK
@File: 069_xpath_解析
@Project: demo01
"""
from lxml import etree

# xpath解析
# （1）本地文件       etree.parse
# （2）服务器响应文件    etree.HTML

# 本地文件
tree = etree.parse('../temp/070_xpath_test.html')
# 查找ul下面的li
li_list = tree.xpath('//ul/li')
print(li_list)

# 查找所有有ID属性的li标签
# text()获取内容
li_list = tree.xpath('//ul/li[@id]/text()')
print(li_list)

# 查找ID是l1的li标签
li_list = tree.xpath('//ul/li[@id="l1"]/text()')
print(li_list)

# 查找ID为l1的li标签的class的属性值
li_list = tree.xpath('//ul/li[@id="l1"]/@class')
print(li_list)

# 查找ID中包含l的li标签
li_list = tree.xpath('//ul/li[contains(@id, "l")]/text()')
print(li_list)

# 查找ID以l开头的li标签
li_list = tree.xpath('//ul/li[starts-with(@id, "l")]/text()')
print(li_list)

li_list = tree.xpath('//ul/li[@id="l1" and @class="c1"]/text()')
print(li_list)

li_list = tree.xpath('//ul/li[@id="l1"]/text() | //ul/li[@id="l2"]/text()')
print(li_list)